/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.sms.dao.StoreDiagnoseTemplateDetailDao;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseTemplateDetail;

/**
 * 店面诊断模板详细Service
 * @author liujiang
 * @version 2013-08-01
 */
@Component
@Transactional(readOnly = true)
public class StoreDiagnoseTemplateDetailService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreDiagnoseTemplateDetailService.class);
	
	@Autowired
	private StoreDiagnoseTemplateDetailDao storeDiagnoseTemplateDetailDao;
	
	public StoreDiagnoseTemplateDetail get(Long id) {
		return storeDiagnoseTemplateDetailDao.findOne(id);
	}
	
	public Page<StoreDiagnoseTemplateDetail> find(Page<StoreDiagnoseTemplateDetail> page, Long parentId, String searchName) {
		DetachedCriteria dc = storeDiagnoseTemplateDetailDao.createDetachedCriteria();
		if (parentId != null){
			dc.add(Restrictions.eq("storeDiagnoseTemplate.id", parentId));
		}
		if (StringUtils.isNotEmpty(searchName)){
			dc.add(Restrictions.like("item_group", "%"+searchName+"%"));
		}
		dc.addOrder(Order.asc("id"));
		return storeDiagnoseTemplateDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreDiagnoseTemplateDetail storeDiagnoseTemplateDetail) {
		storeDiagnoseTemplateDetailDao.save(storeDiagnoseTemplateDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(StoreDiagnoseTemplateDetail e) {
		storeDiagnoseTemplateDetailDao.delete(e);
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId, Long parentId, String searchName) {
		DetachedCriteria dc = storeDiagnoseTemplateDetailDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		if(parentId !=null) {
			dc.add(Restrictions.eq("storeDiagnoseTemplate.id", parentId));
		}
		if (StringUtils.isNotEmpty(searchName)){
			dc.add(Restrictions.like("item_group", "%"+searchName+"%"));
		}
		dc.addOrder(Order.asc("id"));
		return (int)storeDiagnoseTemplateDetailDao.count(dc);
	}
}
